package com.ccnf.web.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ccnf.core.bean.Response;
import com.ccnf.core.bean.ResponseWithData;
import com.ccnf.core.bean.ResponseWithPage;
import com.ccnf.model.entity.*;
import com.ccnf.service.InvitationRecordService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(value = "InvitationRecordController", description = "邀请函统计记录数据相关API")
@RestController
@RequestMapping("/invitations")
public class InvitationRecordController {
    @Autowired
    private InvitationRecordService invitationRecordService;

    @ApiOperation("通过邀请函ID获取邀请函的今日和总的统计数据")
    @GetMapping("{invitationId}/count")
    public ResponseWithData get(@PathVariable Long invitationId) {
        InvitationRecord invitationRecord = new InvitationRecord();
        invitationRecord.setInvitationId(invitationId);
        return ResponseWithData.success(invitationRecordService.selectList(new EntityWrapper<>(invitationRecord)));
    }

    @ApiOperation("通过邀请函ID获取邀请函的今日或者总的统计数据")
    @GetMapping("{invitationId}/record")
    public ResponseWithData record(@PathVariable Long invitationId,
                                   @ApiParam(name = "recordType", value = "0：总，1：今日，默认0")
                                   @RequestParam(required = false, defaultValue = "0") Integer recordType) {
        InvitationRecord invitationRecord = new InvitationRecord();
        invitationRecord.setInvitationId(invitationId);
        invitationRecord.setRecordType(recordType);
        return ResponseWithData.success(invitationRecordService.selectOne(new EntityWrapper<>(invitationRecord)));
    }

    @ApiOperation("通过用户ID获取所有邀请函的总统计（今日和总的统计数据）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "当前登录用户ID", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "0：浏览，1：登记，2：入场，默认0", dataType = "Integer", paramType = "query")
    })
    @GetMapping("record/total/count")
    public ResponseWithData total(Long userId,
                                  @RequestParam(required = false) Long expoId,
                                  @RequestParam(required = false, defaultValue = "0") Integer type) {
        return ResponseWithData.success(invitationRecordService.findTotalRecordByUserId(expoId, userId, type));
    }

    @ApiOperation("通过用户ID获取用户创建邀请函的所有统计数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "当前登录用户ID", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "recordType", value = "0：总，1：今日，默认0", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "0：浏览，1：登记，2：入场，3：关注，默认0", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "当前页，默认1", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示几条，默认5", dataType = "Integer", paramType = "query")
    })
    @GetMapping("record/count")
    public ResponseWithPage list(Long userId,
                                 @RequestParam(required = false) Long expoId,
                                 @RequestParam(required = false, defaultValue = "0") Integer recordType,
                                 @RequestParam(required = false, defaultValue = "0") Integer type,
                                 @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                 @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        page.setSearchCount(false);
        page = invitationRecordService.findRecordsByUserId(page, expoId, userId, recordType, type);
        return ResponseWithPage.success(page.getRecords(), pageNum, pageSize);
    }

    @ApiOperation("查看创建者排行数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "expoId", value = "展会ID", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "当前登录用户ID", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "0：浏览，1：登记，2：入场，3：关注，默认0", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "当前页，默认1", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示几条，默认5", dataType = "Integer", paramType = "query")
    })
    @GetMapping("exhibitor/record/count")
    public ResponseWithPage exhibitorList(Long userId,
                                          @RequestParam(required = false) Long expoId,
                                          @RequestParam(required = false, defaultValue = "0") Integer type,
                                          @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                          @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        page.setSearchCount(false);
        page = invitationRecordService.findAllExhibitorRecordsByUserId(page, expoId, userId, type);
        return ResponseWithPage.success(page.getRecords(), pageNum, pageSize);
    }

    @ApiOperation("用户关注自身邀请函的统计数据, 请传递invitationId、isStar")
    @PostMapping("star/count")
    public Response star(@RequestBody InvitationRecord invitationRecord) {
        invitationRecordService.modifyStarStatus(invitationRecord);
        return Response.success();
    }

    @ApiOperation("创建者和客户页排行关注功能接口")
    @PostMapping("star/user")
    public Response starUser(@RequestBody StarRecord starRecord) {
        invitationRecordService.saveUserStar(starRecord);
        return Response.success();
    }

    @ApiOperation("记录邀请函用户登记状态")
    @PostMapping("user/register")
    public Response register(@RequestBody InvitationRegister invitationRegister) {
        invitationRecordService.saveInvitationUserRegister(invitationRegister);
        return Response.success();
    }

    @ApiOperation("记录邀请函用户入场状态")
    @GetMapping("user/enter")
    public Response enter(String registerId) {
        invitationRecordService.updateInvitationUserEnter(registerId);
        return Response.success();
    }

    @ApiOperation("查看邀请函用户排行数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "当前登录用户ID", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "当前页，默认1", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示几条，默认5", dataType = "Integer", paramType = "query")
    })
    @GetMapping("{invitationId}/users/page")
    public ResponseWithPage usersWithPage(@PathVariable Long invitationId,
                                  Long userId,
                                  @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                  @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        page.setSearchCount(false);
        page = invitationRecordService.findInvitationUserList(page, invitationId, userId);
        return ResponseWithPage.success(page.getRecords(), pageNum, pageSize);
    }

    @ApiOperation("查看邀请函所有用户排行数据，不带分页")
    @GetMapping("{invitationId}/users")
    public ResponseWithData users(@PathVariable Long invitationId,
                                  Long userId) {
        return ResponseWithData.success(invitationRecordService.findInvitationAllUserList(invitationId, userId));
    }

    @ApiOperation("查看邀请函报表数据（浏览、登记、入场、收藏）月和周的统计数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "invitationId", value = "邀请函ID", required = true, paramType = "path"),
            @ApiImplicitParam(name = "reportType", value = "0：浏览，1：登记，2：入场，3：收藏，默认0", defaultValue = "0", paramType = "query"),
            @ApiImplicitParam(name = "timeType", value = "0：月，1：周，默认0", defaultValue = "0", paramType = "query")
    })
    @GetMapping("{invitationId}/report")
    public ResponseWithData month(@PathVariable("invitationId") Long invitationId,
                                  @RequestParam(required = false, defaultValue = "0") Integer reportType,
                                  @RequestParam(required = false, defaultValue = "0") Integer timeType) {
        return ResponseWithData.success(invitationRecordService.findInvitationReportByInvitationId(invitationId, reportType, timeType));
    }

    @ApiOperation("通过被引用的邀请函D查询用户排名列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "invitationId", value = "邀请函ID", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "recordType", value = "0：总，1：今日，默认0", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "0：浏览，1：登记，2：入场，默认0", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "date", value = "查询日排行的时间，默认是当天时间", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "当前页，默认1", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示几条，默认5", dataType = "Integer", paramType = "query")
    })
    @GetMapping("{invitationId}/orientation/rank")
    public ResponseWithPage orientationRank(@PathVariable("invitationId") Long invitationId,
                                            @RequestParam(required = false, defaultValue = "0") Integer recordType,
                                            @RequestParam(required = false) Long date,
                                            @RequestParam(required = false, defaultValue = "0") Integer type,
                                            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                            @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        page.setSearchCount(false);
        page = invitationRecordService.findInvitationOrientationRank(invitationId, type, recordType, date, page);
        return ResponseWithPage.success(page.getRecords(), pageNum, pageSize);
    }

    @ApiOperation("通过被引用的邀请函D查询当前用户排名")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "invitationId", value = "邀请函ID", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "userId", value = "当前登录用户ID", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "recordType", value = "0：总，1：今日，默认0", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "0：浏览，1：登记，2：入场，默认0", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "date", value = "查询日排行的时间，默认是当天时间", dataType = "Integer", paramType = "query")
    })
    @GetMapping("{invitationId}/users/{userId}/orientation/rank")
    public ResponseWithData orientationRank(@PathVariable("invitationId") Long invitationId,
                                            @PathVariable("userId") Long userId,
                                            @RequestParam(required = false, defaultValue = "0") Integer recordType,
                                            @RequestParam(required = false, defaultValue = "0") Integer type,
                                            @RequestParam(required = false) Long date) {
        return ResponseWithData.success(invitationRecordService.findInvitationOrientationCurrentUserRank(invitationId, userId, type, recordType, date));
    }

    @ApiOperation("通过被引用的邀请函D查询用户排名列表，用于实物领奖用户名单查看")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "referenceId", value = "邀请函ID", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "type", value = "0：浏览，1：登记，2：入场，默认0", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "当前页，默认1", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示几条，默认5", dataType = "Integer", paramType = "query")
    })
    @GetMapping("{referenceId}/rank")
    public ResponseWithPage recordRank(@PathVariable Long referenceId,
                                       Long endTime,
                                       @RequestParam(required = false, defaultValue = "0") Integer type,
                                       @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                       @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        page.setSearchCount(false);
        page = invitationRecordService.findInvitationRecordRank(referenceId, type, endTime, page);
        return ResponseWithPage.success(page.getRecords(), pageNum, pageSize);
    }
}